package cn.tb.ymcc.service.impl;

import cn.tb.ymcc.domain.BlacklistSms;
import cn.tb.ymcc.domain.MessageSms;
import cn.tb.ymcc.dto.PullBlackDto;
import cn.tb.ymcc.mapper.MessageSmsMapper;
import cn.tb.ymcc.service.IBlacklistSmsService;
import cn.tb.ymcc.service.IMessageSmsService;
import cn.tb.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tangbo
 * @since 2022-07-15
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements IMessageSmsService {
    @Autowired
    private IBlacklistSmsService blacklistSmsService;
    @Autowired
    private IMessageSmsService messageSmsService;
    @Autowired
    private MessageSmsMapper messageSmsMapper;
    /**
     * 拉黑
     * 1.参数校验
     * 2.存入黑名单
     * @param dto
     */
    @Override
    public void pullBlack(PullBlackDto dto) {
        BlacklistSms blacklistSms = blacklistSmsService.loadByMobile(dto.getMobile());
        AssertUtil.isNull(blacklistSms,"黑名单号码！！");

        MessageSms messageSms = selectById(dto.getId());
        messageSms.setState(1);
        EntityWrapper<MessageSms> wrapper = new EntityWrapper();
        Wrapper<MessageSms> smsWrapper = wrapper.eq("id", dto.getId());
        update(messageSms,smsWrapper);

        blacklistSms = saveBlacklist(dto);
        blacklistSmsService.insert(blacklistSms);
    }

    private BlacklistSms saveBlacklist(PullBlackDto dto) {
        BlacklistSms blacklistSms = new BlacklistSms();
        blacklistSms.setMobile(dto.getMobile());
        blacklistSms.setCreatTime(new Date());
        blacklistSms.setIp(dto.getIp());
        blacklistSms.setReason(dto.getReason());
        return blacklistSms;
    }

    public MessageSms selectByMobile(String mobile) {
        return messageSmsMapper.selectByMobile(mobile);
    }
}
